import express from 'express';

var router = express.Router();

/**
 *  登录
 **/
router.post('/login', function (req, res) {
    var loginUser = { name: req.body.name, password: req.body.password }
    //req.session.id = req.body.sessionid;
    console.log(loginUser);
    if (loginUser) {
        req.session.loginUser = req.body.name;  // 将用户信息写入 session
        if (req.session.originalUrl) {  // 如果存在原始请求路径，将用户重定向到原始请求路径
            var redirectUrl = req.session.originalUrl;
            req.session.originalUrl = null;  // 清空 session 中存储的原始请求路径
        } else {  // 不存在原始请求路径，将用户重定向到根路径
            var redirectUrl = '/';
        }
        return res.json({ ret_code: 1, ret_msg: '登录成功' });
    } else {
        res.json({ ret_code: 1, ret_msg: '账号或密码错误' });
    }
    function redomToken(len) {
        var len = len || 32;
        var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
        var maxpos = chars.length;
        var str = '';

        for (var i = 0; i < len; i++) {
            str += chars.charAt(Math.floor(Math.random() * maxpos))
        }

        return str;
    }
});

/**
 *  退出
 **/
router.post('/signout', function (req, res) {
    req.session.token = null;
    res.send({
        c: 200,
        m: '感谢使用！'
    });
});

export default router;